package com.md.api.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 *
 * 时间工具类
 * 
 * @author 80002202
 *
 */
public class DateUtils {

	private DateUtils() {
	}

	public final static SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyy-MM-dd");
	public final static SimpleDateFormat yyyyMMddFormat = new SimpleDateFormat("yyyyMMdd");
	public final static SimpleDateFormat yyyyMMddHHmmssFormat = new SimpleDateFormat("yyyyMMddHHmmss");
	public final static SimpleDateFormat yyyyMMddHHmmss = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	/**
	 * 例：2018-01-01
	 */
	public final static String tF = "%tF";

	/**
	 * 例：2018-01-01 11:11:22
	 */
	public final static String tFT = "%tT";

	/**
	 * 将java.util.Date转换为相关格式字符串
	 * 
	 * @return
	 */
	public static String format(Date date, SimpleDateFormat dateFormat) {
		synchronized (dateFormat) {
			return dateFormat.format(date);
		}
	}

	/**
	 * 将java.util.Date转换为相关格式字符串
	 * 
	 * @return
	 */
	public static String format(Date date, String format) {
		return String.format(format, date);
	}

	/**
	 * 将字符串转为 java.util.Date 类型
	 * 
	 * @return
	 * @throws ParseException
	 */
	public static Date parse(String source, SimpleDateFormat dateFormat) {
		synchronized (dateFormat) {
			try {
				return dateFormat.parse(source);
			} catch (ParseException e) {
				return null;
			}
		}
	}

	/**
	 * 获取系统当前日期
	 * 
	 * @return
	 */
	public static Date getNowDate() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}

	/**
	 * 获取系统当前时间
	 * 
	 * @return
	 */
	public static Date getNowDateTime() {
		Calendar calendar = Calendar.getInstance();
		return calendar.getTime();
	}

	public static Date add(Date time, int field, int amount) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(time);
		calendar.add(field, amount);
		return calendar.getTime();
	}

	public static Date set(Date time, int field, int amount) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(time);
		calendar.set(field, amount);
		return calendar.getTime();
	}

	public static Calendar getCalendar(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar;
	}

	public static Date secondToDate(Long millis) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(millis * 1000);
		return calendar.getTime();
	}

	public static int getDatePoordDay(Date endDate, Date nowDate) {

		long nd = 1000 * 24 * 60 * 60;
		// long ns = 1000;
		// 获得两个时间的毫秒时间差异
		long diff = endDate.getTime() - nowDate.getTime();
		// 计算差多少天
		long day = diff / nd;
		int i = Integer.parseInt(String.valueOf(day));
		return i;
	}

	public static long getDatePoordHour(Date endDate, Date nowDate) {

		long nd = 1000 * 24 * 60 * 60;
		long nh = 1000 * 60 * 60;
		// 获得两个时间的毫秒时间差异
		long diff = endDate.getTime() - nowDate.getTime();
		// 计算差多少小时
		long hour = diff % nd / nh;
		return hour;
	}

	public static long getDatePoordMin(Date endDate, Date nowDate) {

		long nd = 1000 * 24 * 60 * 60;
		long nh = 1000 * 60 * 60;
		long nm = 1000 * 60;
		// long ns = 1000;
		// 获得两个时间的毫秒时间差异
		long diff = endDate.getTime() - nowDate.getTime();
		// 计算差多少分钟
		long min = diff % nd % nh / nm;
		// 计算差多少秒//输出结果
		// long sec = diff % nd % nh % nm / ns;
		return min;
	}
	/***
	 * @comments 计算两个时间的时间差
	 * @param strTime1
	 * @param strTime2
	 */
	public static void getTimeDifference(String strTime1,String strTime2) {
		//格式日期格式，在此我用的是"2018-01-24 19:49:50"这种格式
		//可以更改为自己使用的格式，例如：yyyy/MM/dd HH:mm:ss 。。。
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		   try{
			   Date now = df.parse(strTime1);
			   Date date=df.parse(strTime2);
			   long l=now.getTime()-date.getTime();       //获取时间差
			   long day=l/(24*60*60*1000);
			   long hour=(l/(60*60*1000)-day*24);
			   long min=((l/(60*1000))-day*24*60-hour*60);
			   long s=(l/1000-day*24*60*60-hour*60*60-min*60);
			   System.out.println(""+day+"天"+hour+"小时"+min+"分"+s+"秒");
		   }catch(Exception e){
			   e.printStackTrace();
		   }
	}

	//获取指定年份之间的所有年份
	public static List<String> getAllYear(Integer startYear,Integer endYear) {
		if(null==endYear){
			Calendar cal = Calendar.getInstance();
			Integer toYear = cal.get(Calendar.YEAR);
			Integer section = toYear-startYear;  // 声明一个变量 获得当前年份至想获取年份差 eg.2008
			List<String> arrYear = new ArrayList<String>(); // 声明一个空数组 把遍历出的年份添加到数组里
			for(int i = 0;i<=section;i++){
				Integer year1  = toYear-i;
				arrYear.add(year1.toString());
			}
			return arrYear;
		}else{
			Integer section = endYear-startYear;  // 声明一个变量 获得当前年份至想获取年份差 eg.2008
			List<String> arrYear = new ArrayList<String>(); // 声明一个空数组 把遍历出的年份添加到数组里
			for(int i = 0;i<=section;i++){
				Integer year1  = endYear-i;
				arrYear.add(year1.toString());
			}
			return arrYear;
		}
	}

	/***
	 * @comments 测试使用
	 * @param args
	 */
	public static void main(String[] args) {
//		String strTime1 = "2018-6-30 19:49:50";
//		String strTime2 = "2017-09-17 10:58:40";
//		getTimeDifference(strTime1, strTime2);
		System.out.println(getAllYear(2012,2018));
	}
}
